const Validator = require('validator');
const isEmpty = require('./isEmpty')

module.exports = function validationRegIpt(data){
  let errors = {};
  data.name = !isEmpty(data.name) ? data.name : '';
  data.email = !isEmpty(data.email) ? data.email : '';
  data.password = !isEmpty(data.password) ? data.password : '';
  data.password2 = !isEmpty(data.password2) ? data.password2 : '';

  if(!Validator.isLength(data.name, { min:2, max:16 })){
    errors.name = "名字的长度不能小于2位且不能大于16位";
  }
  // 这个isEmpty是Validator的方法
  if(Validator.isEmpty(data.name)){
    errors.name = "名字不能为空"
  }
  if(!Validator.isEmail(data.email)){
    errors.email= "邮箱不合法"
  }
  if(Validator.isEmpty(data.email)){
    errors.email = "邮箱不能为空"
  }
  if(Validator.isEmpty(data.password)){
    errors.password = "密码不能为空"
  }
  if(!Validator.isLength(data.password, { min:6, max:16 })){
    errors.password = "名字的长度不能小于6位且不能大于16位"
  }
  if(Validator.isEmpty(data.password2)){
    errors.password2 = "密码不能为空"
  }
  if(!Validator.equals(data.password, data.password2)){
    errors.password2 = "两次密码不一致"
  }

  return {
    errors: errors,
    // 注意这个isEmpty 是自己写的方法
    isValid: isEmpty(errors),
  }
}